package com.hsgene.order.constants.v1;

import com.google.common.collect.Lists;
import com.hsgene.common.util.exception.IllegalParamException;

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * 金琉璃APP状态分类
 *
 * @author wangbing
 * @version 1.0, 2018/9/19
 */
public enum DoctorAppStatus {
    /**
     * 全部
     */
    ALL("0", Lists.newArrayList(OrderStatus.values())),

    /**
     * 已提交
     */
    SUBMITTED("1", Lists.newArrayList(OrderStatus.INIT)),

    /**
     * 待付款
     */
    WAIT_PAYMENT("2", Lists.newArrayList(OrderStatus.WAIT_RECEIPT_MONEY, OrderStatus.WAIT_RECEIPT_DISTRIBUTION)),

    /**
     * 检测中
     */
    TESTING("3", Lists.newArrayList(OrderStatus.DIFU_CONFIRMED, OrderStatus.WAIT_PERFECTED, OrderStatus.WAIT_SAMPLE_CONFIRM, OrderStatus.TESTING)),

    /**
     * 已出报告
     */
    REPORTED("4", Lists.newArrayList(OrderStatus.PAID_FOR, OrderStatus.PENDING_PAYMENT, OrderStatus.MAIL, OrderStatus.BILL_CONFIRMED, OrderStatus.WAIT_AGENCY_SEND, OrderStatus.WAIT_DIFU_SIGNING, OrderStatus.DONE)),

    /**
     * 取消
     */
    CANCELED("5", Lists.newArrayList(OrderStatus.CANCELED)),

    /**
     * 终止
     */
    STOP("6", Lists.newArrayList(OrderStatus.STOP)),

    /**
     * 出现异常
     */
    EXCEPTION("7", Lists.newArrayList(OrderStatus.EXCEPTION));

    private final String value;

    private final List<OrderStatus> statuses;

    DoctorAppStatus(String code, List<OrderStatus> statuses) {
        this.value = code;
        this.statuses = statuses;
    }

    public static DoctorAppStatus getEnum(String value) {
        for (DoctorAppStatus status : values()) {
            if (status.getValue().equals(value)) {
                return status;
            }
        }

        throw new IllegalParamException("非法状态分类value值：" + value);
    }

    public static Optional<DoctorAppStatus> parseOrderStatus(OrderStatus orderStatus) {
        return Stream.of(values())
                .filter(e -> !ALL.equals(e) && e.getStatuses().contains(orderStatus)).findFirst();
    }

    public String getValue() {
        return value;
    }

    public List<OrderStatus> getStatuses() {
        return statuses;
    }
}
